
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import org.workplicity.entry.WorkSlate;
import org.workplicity.task.NetTask;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TableForm.java
 *
 * Created on Feb 23, 2011, 7:20:07 PM
 */

/**
 *
 * @author Marist User
 */
public class TableForm extends javax.swing.JFrame {

    /** Creates new form TableForm */
    public TableForm() {
        initComponents();

        init();
    }

    private void init() {
        workSlateTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    JTable target = (JTable) e.getSource();
                    int row = target.getSelectedRow();
                    //int column = target.getSelectedColumn();
                    editRequest(row);
                }
            }
        });
    }
    private void editRequest(int row) {
        WorkSlateTableModel model = (WorkSlateTableModel) workSlateTable.getModel();

        WorkSlate workSlate = model.getRow(row);

        System.out.println(workSlate);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        workSlateTable = new javax.swing.JTable();
        doneButton = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        workSlateTable.setModel(new WorkSlateTableModel( ));
        jScrollPane1.setViewportView(workSlateTable);

        doneButton.setText("Done");
        doneButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doneButtonActionPerformed(evt);
            }
        });

        refreshButton.setText("Refresh");
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(15, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(refreshButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(doneButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(doneButton)
                    .addComponent(refreshButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed
        WorkSlateTableModel model = (WorkSlateTableModel) workSlateTable.getModel();

        final HashMap<Integer, WorkSlate> dirty = model.getDirty();

        if (model.getDirty().isEmpty()) {
            dispose();
            return;
        }

        String msg = "Some work slates have changed.";
        msg += "\nSave them?";

        int n = JOptionPane.showConfirmDialog(
                this,
                msg,
                "Confirm",
                JOptionPane.YES_NO_OPTION);

        if (n == 1) {
            dispose();
            return;
        }

        final TableForm frame = this;

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                WorkletContext context = WorkletContext.getInstance();
                
                for(Integer id : dirty.keySet()) {
                    WorkSlate workSlate = dirty.get(id);
                    
                    if(!Helper.insert(workSlate, NetTask.REPOS_WORKSLATES,context)) {
                        JOptionPane.showMessageDialog(frame, "Saved failed!",
                                "Grounds", JOptionPane.ERROR_MESSAGE);
                        break;
                    }
                }
                
                dispose();                
            }

        });

    }//GEN-LAST:event_doneButtonActionPerformed

    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
        final TableForm frame = this;
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                WorkSlateTableModel model = (WorkSlateTableModel) workSlateTable.getModel();

                final HashMap<Integer, WorkSlate> dirty = model.getDirty();

                if (model.getDirty().isEmpty()) {
                    model.refresh();
                    return;
                }

                String msg = "Some work slates have changed.";
                msg += "\nSave them?";

                int n = JOptionPane.showConfirmDialog(
                        frame,
                        msg,
                        "Confirm",
                        JOptionPane.YES_NO_OPTION);

                if (n == 1) {
                    model.refresh();
                    return;
                }

                WorkletContext context = WorkletContext.getInstance();
                
                for(Integer id : dirty.keySet()) {
                    WorkSlate workSlate = dirty.get(id);

                    if(!Helper.insert(workSlate, NetTask.REPOS_WORKSLATES,context)) {
                        JOptionPane.showMessageDialog(frame, "Saved failed!",
                                "Grounds", JOptionPane.ERROR_MESSAGE);
                        break;
                    }
                }

                model.refresh();
            }
        });
    }//GEN-LAST:event_refreshButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TableForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton doneButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton refreshButton;
    private javax.swing.JTable workSlateTable;
    // End of variables declaration//GEN-END:variables

}
